package com.hertz.iris.view.alert;

import javax.inject.Inject;

import android.os.Bundle;
import android.os.PowerManager.WakeLock;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.Button;
import butterknife.InjectView;
import butterknife.OnClick;

import com.hertz.iris.Injector;
import com.hertz.iris.R;
import com.hertz.iris.core.NormalAlarmService;
import com.hertz.iris.dao.NormalAlarmDao;
import com.hertz.iris.pojo.NormalAlarm;
import com.hertz.iris.ui.IrisActivity;
import com.hertz.iris.util.Constants;
import com.hertz.iris.util.MediaPlayerHelper;

public class NormalAlarmAlertActivity extends IrisActivity {

	@InjectView(R.id.snooze_btn)
	Button snoozeBtn;
	@InjectView(R.id.stop_btn)
	Button stopBtn;
	@Inject
	NormalAlarmService normalAlarmService;
	private NormalAlarm na;
	WakeLock wakeLock;
	
	private MediaPlayerHelper mediaPlayerHelper;
	
	@Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        Injector.inject(this);
        
        AlarmAlertWakeLock.acquire(this);
        setContentView(R.layout.normal_alert); 
        
//        // TODO clearFlags
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
                | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON 
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        na = (NormalAlarm)getIntent().getSerializableExtra(NormalAlarmDao.TABLENAME);
        mediaPlayerHelper = new MediaPlayerHelper(this);
        mediaPlayerHelper.playRingtone(na.getRingUri(), (float)na.getVolume() / 100,na.getWave());
    }
	
	@OnClick(R.id.stop_btn)
	void stopBtnOnClick(){
		mediaPlayerHelper.stopRingtone();
		AlarmAlertWakeLock.release();
		if(!na.getWeeks().equals(Constants.NORMAL_ALARM.DEFAULT_WEEKS)){
			normalAlarmService.resetAlarmActive(na);
		}else{
			na.setIsActive(false);
			normalAlarmService.updateNormalAlarm(na);
		}
		finish();
	}
	
	@OnClick(R.id.snooze_btn)
	void snoozeBtnOnClick(){
		mediaPlayerHelper.stopRingtone();
		normalAlarmService.snooze(na);
		AlarmAlertWakeLock.release();
		finish();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	@Override 
	public boolean onKeyDown(int keyCode, KeyEvent event) { 
	    if(keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键 
	        return true; 
	    }   
	    return super.onKeyDown(keyCode, event); 
	}
}
